package com.byedu.course;

import java.util.*;

import com.byedu.domain.course.AnswerDO;
import com.byedu.domain.course.LibraryDO;
import com.byedu.domain.course.UserQuestionDO;
import com.byedu.domain.course.vo.QuestionVO;
import com.byedu.domain.course.vo.UserQuestionVO;
import com.byedu.mapper.course.AnswerMapper;
import com.byedu.model.exception.ApiException;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.byedu.mapper.course.UserQuestionMapper;
import com.byedu.util.Constants;

/**
 * Created by Luo on 2017-04-27.
 */
@Service
public class UserQuestionService {
    @Autowired
    private UserQuestionMapper userQuestionMapper;

    @Autowired
    private AnswerMapper answerMapper;

    /**
     * @description 获取用户答题卡
     * @author luo
     * @date 2017-04-27
     * @param userId
     * @return
     */
    public List<UserQuestionVO> listUserQuestionsByUserId(String userId) {
        return convertDoListToVoList(userQuestionMapper.listUserQuestionsByUserId(userId));
    }

    public List<UserQuestionVO> convertDoListToVoList(List<UserQuestionDO> userQuestionDOList) {
        if (CollectionUtils.isNotEmpty(userQuestionDOList)) {
            List<UserQuestionVO> userQuestionVOList = new ArrayList<UserQuestionVO>();
            for (UserQuestionDO userQuestionDO : userQuestionDOList) {
                UserQuestionVO courseVO = new UserQuestionVO(userQuestionDO);
                userQuestionVOList.add(courseVO);
            }

            return userQuestionVOList;
        }

        return null;
    }

    /**
     * @description 获取用户答题卡
     * @author luo
     * @date 2017-04-27
     * @param userId
     * @param questionId
     * @return
     */
    public void  delete(String userId, Integer questionId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Constants.USER_ID, userId);
        map.put(Constants.QUESTION_ID, questionId);
       userQuestionMapper.deleteByUserIdAndQuestionId(map);
    }

    public int insertOrupdate(UserQuestionDO userQuestion){

        List<UserQuestionDO> userQuestionsData = userQuestionMapper.listUserQuestions(userQuestion);
        int resultNo = 0;
        for (UserQuestionDO question : userQuestionsData) {
        	if(question != null){
                resultNo = userQuestionMapper.updateUserQuestion(question);
            }else{
                userQuestionMapper.insertUserQuestion(question);
            }
        }



        return resultNo;
    }

    /**
     * @description 录入用户答题记录
     * @author chenxigui
     * @date 2017-04-29
     * @param userId
     * @param questionId
     * @param subOptions
     * @return
     */
    public void manage(String userId, Integer questionId, String subOptions) {
        if (userId != null && questionId != null && !StringUtils.isEmpty(subOptions)) {
            //1.删除答题卡中对应的信息
            delete(userId, questionId);
            //2.直接将用户新提交的答题记录插入数据库
            List<UserQuestionDO> userQuestions = packageUserQuestions(userId, questionId, subOptions);
            userQuestionMapper.insertUserQuestionBatch(userQuestions);
        }

    }

    /**
     * @description 封装数据到实体
     * @param userId
     * @param questionId
     * @param subOptions
     * @return
     */
    public List<UserQuestionDO> packageUserQuestions(String userId, Integer questionId, String subOptions) {
        List<UserQuestionDO> userQuestions = new ArrayList<UserQuestionDO>();
        String[] subOptionAry = subOptions.split(Constants.EN_COMMA);
        java.text.DateFormat format1 = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String createTime = format1.format(new Date());
        for (String subOption : subOptionAry) {
            UserQuestionDO userQuestion = new UserQuestionDO(questionId, userId, subOption, createTime);
            userQuestions.add(userQuestion);
        }

        return userQuestions;
    }
}
